package com.zhoug.common.utils.bundle;

import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.util.Size;
import android.util.SizeF;
import android.util.SparseArray;

import com.zhoug.common.utils.ClassUtils;
import com.zhoug.common.utils.LogUtils;

import java.io.Serializable;
import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

/**
 * @Author: zhoug
 * @Date: 2025-04-16
 * @Description:
 */
public class BundleCompat implements IBundle {
    private static final String TAG = ">>>BundleCompat";

    private final Bundle mBundle;

    protected BundleCompat(Bundle bundle) {
        this.mBundle = bundle;
    }


    public static IBundle with(Bundle bundle) {
        return new BundleCompat(bundle);
    }

    @Override
    public Bundle getData() {
        return mBundle;
    }

    @Override
    public IBundle putInt(@Nullable String key, int value) {
        if (mBundle != null) {
            mBundle.putInt(key, value);
        }
        return this;
    }

    @Override
    public IBundle putLong(@Nullable String key, long value) {
        if (mBundle != null) {
            mBundle.putLong(key, value);
        }
        return this;
    }

    @Override
    public IBundle putDouble(@Nullable String key, double value) {
        if (mBundle != null) {
            mBundle.putDouble(key, value);
        }
        return this;
    }

    @Override
    public IBundle putBoolean(@Nullable String key, boolean value) {
        if (mBundle != null) {
            mBundle.putBoolean(key, value);
        }
        return this;
    }

    @Override
    public IBundle putString(@Nullable String key, String value) {
        if (mBundle != null) {
            mBundle.putString(key, value);
        }
        return this;
    }

    @Override
    public IBundle putByte(@Nullable String key, byte value) {
        if (mBundle != null) {
            mBundle.putByte(key, value);
        }
        return this;
    }

    @Override
    public IBundle putChar(@Nullable String key, char value) {
        if (mBundle != null) {
            mBundle.putChar(key, value);
        }
        return this;
    }

    @Override
    public IBundle putShort(@Nullable String key, short value) {
        if (mBundle != null) {
            mBundle.putShort(key, value);
        }
        return this;

    }

    @Override
    public IBundle putFloat(@Nullable String key, float value) {
        if (mBundle != null) {
            mBundle.putFloat(key, value);
        }
        return this;

    }

    @Override
    public IBundle putCharSequence(@Nullable String key, @Nullable CharSequence value) {
        if (mBundle != null) {
            mBundle.putCharSequence(key, value);
        }
        return this;

    }

    @Override
    public IBundle putParcelable(@Nullable String key, @Nullable Parcelable value) {
        if (mBundle != null) {
            mBundle.putParcelable(key, value);
        }
        return this;

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public IBundle putSize(@Nullable String key, @Nullable Size value) {
        if (mBundle != null) {
            mBundle.putSize(key, value);
        }
        return this;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public IBundle putSizeF(@Nullable String key, @Nullable SizeF value) {
        if (mBundle != null) {
            mBundle.putSizeF(key, value);
        }
        return this;
    }

    @Override
    public IBundle putParcelableArray(@Nullable String key, @Nullable Parcelable[] value) {
        if (mBundle != null) {
            mBundle.putParcelableArray(key, value);
        }
        return this;
    }

    @Override
    public IBundle putParcelableArrayList(@Nullable String key, @Nullable ArrayList<? extends Parcelable> value) {
        if (mBundle != null) {
            mBundle.putParcelableArrayList(key, value);
        }
        return this;

    }

    @Override
    public IBundle putSparseParcelableArray(@Nullable String key, @Nullable SparseArray<? extends Parcelable> value) {
        if (mBundle != null) {
            mBundle.putSparseParcelableArray(key, value);
        }
        return this;

    }

    @Override
    public IBundle putIntegerArrayList(@Nullable String key, @Nullable ArrayList<Integer> value) {
        if (mBundle != null) {
            mBundle.putIntegerArrayList(key, value);
        }
        return this;

    }

    @Override
    public IBundle putStringArrayList(@Nullable String key, @Nullable ArrayList<String> value) {
        if (mBundle != null) {
            mBundle.putStringArrayList(key, value);
        }
        return this;
    }

    @Override
    public IBundle putCharSequenceArrayList(@Nullable String key, @Nullable ArrayList<CharSequence> value) {
        if (mBundle != null) {
            mBundle.putCharSequenceArrayList(key, value);
        }
        return this;
    }

    @Override
    public IBundle putSerializable(@Nullable String key, @Nullable Serializable value) {
        if (mBundle != null) {
            mBundle.putSerializable(key, value);
        }
        return this;
    }

    @Override
    public IBundle putSerializableArrayList(@Nullable String key, @Nullable ArrayList<? extends Serializable> value) {
        return putSerializable(key, value);
    }

    @Override
    public IBundle putByteArray(@Nullable String key, @Nullable byte[] value) {
        if (mBundle != null) {
            mBundle.putByteArray(key, value);
        }
        return this;
    }

    @Override
    public IBundle putShortArray(@Nullable String key, @Nullable short[] value) {
        if (mBundle != null) {
            mBundle.putShortArray(key, value);
        }
        return this;
    }

    @Override
    public IBundle putCharArray(@Nullable String key, @Nullable char[] value) {
        if (mBundle != null) {
            mBundle.putCharArray(key, value);
        }
        return this;
    }

    @Override
    public IBundle putFloatArray(@Nullable String key, @Nullable float[] value) {
        if (mBundle != null) {
            mBundle.putFloatArray(key, value);
        }
        return this;
    }

    @Override
    public IBundle putCharSequenceArray(@Nullable String key, @Nullable CharSequence[] value) {
        if (mBundle != null) {
            mBundle.putCharSequenceArray(key, value);
        }
        return this;
    }

    @Override
    public IBundle putBundle(@Nullable String key, @Nullable Bundle value) {
        if (mBundle != null) {
            mBundle.putBundle(key, value);
        }
        return this;
    }

    @Override
    public IBundle putBinder(@Nullable String key, @Nullable IBinder value) {
        if (mBundle != null) {
            mBundle.putBinder(key, value);
        }
        return this;
    }


    @Override
    public int getInt(String key) {
        if (mBundle != null) {
            return  mBundle.getInt(key);
        }
        return 0;
    }

    @Override
    public int getInt(String key, int defaultValue) {
        if (mBundle != null) {
            return  mBundle.getInt(key,defaultValue);
        }
        return defaultValue;
    }

    @Override
    public long getLong(String key) {
        if (mBundle != null) {
            return   mBundle.getLong(key);
        }
        return 0;
    }

    @Override
    public long getLong(String key, long defaultValue) {
        if (mBundle != null) {
            return  mBundle.getLong(key,defaultValue);
        }
        return defaultValue;
    }

    @Override
    public double getDouble(String key) {
        if (mBundle != null) {
            return mBundle.getDouble(key);
        }
        return 0;
    }

    @Override
    public double getDouble(String key, double defaultValue) {
        if (mBundle != null) {
            return  mBundle.getDouble(key,defaultValue);
        }
        return defaultValue;
    }

    @Override
    public boolean getBoolean(String key) {
        if (mBundle != null) {
            return  mBundle.getBoolean(key);
        }
        return false;
    }

    @Override
    public boolean getBoolean(String key, boolean defaultValue) {
        if (mBundle != null) {
            return mBundle.getBoolean(key,defaultValue);
        }
        return defaultValue;
    }

    @Override
    public String getString(String key) {
        if (mBundle != null) {
            return  mBundle.getString(key);
        }
        return null;
    }

    @Override
    public String getString(String key, String defaultValue) {
        if (mBundle != null) {
            return mBundle.getString(key,defaultValue);
        }
        return defaultValue;
    }

    @Override
    public byte getByte(String key) {
        if (mBundle != null) {
            try {
                return mBundle.getByte(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return 0;
    }

    @Override
    public Byte getByte(String key, byte defaultValue) {
        if (mBundle != null) {
            try {
                return mBundle.getByte(key, defaultValue);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Override
    public char getChar(String key) {
        if (mBundle != null) {
            try {
                return mBundle.getChar(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return 0;
    }

    @Override
    public char getChar(String key, char defaultValue) {
        if (mBundle != null) {
            try {
                return mBundle.getChar(key, defaultValue);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return 0;
    }

    @Override
    public short getShort(String key) {
        if (mBundle != null) {
            try {
                return mBundle.getShort(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return 0;
    }

    @Override
    public short getShort(String key, short defaultValue) {
        if (mBundle != null) {
            try {
                return mBundle.getShort(key, defaultValue);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return 0;
    }

    @Override
    public float getFloat(String key) {
        if (mBundle != null) {
            try {
                return mBundle.getFloat(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return 0;
    }

    @Override
    public float getFloat(String key, float defaultValue) {
        if (mBundle != null) {
            try {
                return mBundle.getFloat(key, defaultValue);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return 0;
    }

    @Nullable
    @Override
    public CharSequence getCharSequence(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getCharSequence(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Override
    public CharSequence getCharSequence(@Nullable String key, CharSequence defaultValue) {
        if (mBundle != null) {
            try {
                return mBundle.getCharSequence(key, defaultValue);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public Size getSize(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getSize(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public SizeF getSizeF(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getSizeF(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public Bundle getBundle(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getBundle(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public <T extends Parcelable> T getParcelable(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getParcelable(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public <T> T getParcelable(@Nullable String key, @NonNull Class<T> clazz) {
        if (mBundle != null) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    return mBundle.getParcelable(key, clazz);
                } else {
                    Parcelable parcelable = mBundle.getParcelable(key);
                    if (parcelable != null) {
                        return ClassUtils.castTo(parcelable, clazz);
                    }
                }
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public Parcelable[] getParcelableArray(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getParcelableArray(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }


    @Nullable
    @Override
    public <T> T[] getParcelableArray(@Nullable String key, @NonNull Class<T> clazz) {
        if (mBundle != null) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    return mBundle.getParcelableArray(key, clazz);
                } else {
                    Parcelable[] parcelableArray = mBundle.getParcelableArray(key);
                    if (parcelableArray != null) {
                        return (T[]) parcelableArray;
                    }
                }
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }

        }
        return null;
    }

    @Nullable
    @Override
    public <T extends Parcelable> ArrayList<T> getParcelableArrayList(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getParcelableArrayList(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public <T> ArrayList<T> getParcelableArrayList(@Nullable String key, @NonNull Class<? extends T> clazz) {
        if (mBundle != null) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    return mBundle.getParcelableArrayList(key, clazz);
                } else {
                    ArrayList<Parcelable> parcelableArrayList = mBundle.getParcelableArrayList(key);
                    if (parcelableArrayList != null) {
                        return (ArrayList<T>) parcelableArrayList;
                    }
                }
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }

        }
        return null;
    }

    @Nullable
    @Override
    public <T extends Parcelable> SparseArray<T> getSparseParcelableArray(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getSparseParcelableArray(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public <T> SparseArray<T> getSparseParcelableArray(@Nullable String key, @NonNull Class<? extends T> clazz) {
        if (mBundle != null) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    return mBundle.getSparseParcelableArray(key, clazz);
                } else {
                    SparseArray<Parcelable> sparseParcelableArray = mBundle.getSparseParcelableArray(key);
                    if (sparseParcelableArray != null) {
                        return (SparseArray<T>) sparseParcelableArray;
                    }
                }
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public Serializable getSerializable(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getSerializable(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public <T extends Serializable> T getSerializable(@Nullable String key, @NonNull Class<T> clazz) {
        if (mBundle != null) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    return mBundle.getSerializable(key, clazz);
                } else {
                    Serializable serializable = mBundle.getSerializable(key);
                    if (serializable != null) {
                        return (T) serializable;
                    }
                }
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public <T extends Serializable> ArrayList<T> getSerializableArrayList(@Nullable String key, @NonNull Class<T> clazz) {
        if (mBundle != null) {
            try {
                Serializable serializable = mBundle.getSerializable(key);
                if (serializable != null) {
                    return (ArrayList<T>) mBundle.getSerializable(key);
                }
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public ArrayList<Integer> getIntegerArrayList(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getIntegerArrayList(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public ArrayList<String> getStringArrayList(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getStringArrayList(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public ArrayList<CharSequence> getCharSequenceArrayList(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getCharSequenceArrayList(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public byte[] getByteArray(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getByteArray(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public short[] getShortArray(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getShortArray(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public char[] getCharArray(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getCharArray(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public float[] getFloatArray(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getFloatArray(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public CharSequence[] getCharSequenceArray(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getCharSequenceArray(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Nullable
    @Override
    public IBinder getBinder(@Nullable String key) {
        if (mBundle != null) {
            try {
                return mBundle.getBinder(key);
            } catch (Exception e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
        return null;
    }


}

